package com.zzb.framework.user.event.processor;

import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.ServiceException;
import com.zzb.framework.event.AbstractProcessor;
import com.zzb.framework.user.entity.LoginHistory;
import com.zzb.framework.user.event.msg.LoginSuccessEvent;
import com.zzb.framework.user.service.LoginHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录成功后的测试回调
 * Created by zhangzhenbin on 17-2-24.
 */
@Component("loginSuccessProcessor")
public class LoginSuccessProcessor implements AbstractProcessor<LoginSuccessEvent> {
    private final static Logger logger = LoggerFactory.getLogger(LoginSuccessProcessor.class);
    @Autowired
    private LoginHistoryService loginHistoryService;
    @Override
    public BaseResponse execute(LoginSuccessEvent event) {
        try {
            LoginHistory loginHistory = new LoginHistory();
            loginHistory.setLoginName(event.getUser().getLoginName());
            loginHistory.setLoginIp(event.getIp());
            return loginHistoryService.saveLoginHistory(loginHistory,event.getUser());
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }
}
